<?php

// +----------------------------------------------------------------------
// | Center Plugin for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/think-plugs-center
// | github 代码仓库：https://github.com/zoujingli/think-plugs-center
// +----------------------------------------------------------------------

declare (strict_types=1);

namespace plugin\center\controller;

use plugin\center\service\ApiService;
use plugin\center\service\LoginService;
use think\admin\Controller;

/**
 * 用户登录管理
 * Class Login
 * @package plugin\center\controller
 */
class Login extends Controller
{

    public function check()
    {
    }

    /**
     * 插件用户登录
     * @auth true
     * @return void
     * @throws \think\admin\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function in()
    {
        if ($this->request->isGet()) {
            $info = LoginService::check();
            if ($info['code'] === 'wxqrc') {
                $this->vo = $info;
                $this->fetch('wxqrc');
            } elseif ($info['code'] === 'null') {
                $this->error($info['message']);
            } else {
                $this->fetch('login');
            }
        } else {
            $data = $this->_vali([
                'email.email'    => '电子邮箱格式错误！',
                'email.require'  => '电子邮箱不能为空！',
                'verify.require' => '邮箱验证码不能为空！'
            ]);
            $this->user = ApiService::call('login.bind', $data['email'], $data['verify']);
            if (empty($this->user)) {
                $this->error('登录失败！');
            } else {
                $this->success('登录成功！');
            }
        }
    }

    /**
     * 找回登录密码
     * @auth true
     * @return void
     */
    public function forget()
    {
        $data = $this->_vali([
            'mail.email'   => '邮箱账号格式错误！',
            'mail.require' => '邮箱账号不能为空！',
        ]);
        $this->success('请求成功！', $data);
    }
}